package org.example.aigc.controller;

import org.example.aigc.service.UploadService;
import org.example.aigc.utils.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Objects;

/**
 * 处理上传文件的事务
 */
@RequestMapping("/upload")
@RestController
public class UploadController {

    private final String avatarUploadDir;

    private final String audioUploadDir;

    private final String videoUploadDir;

    private final String pptUploadDir;

    private final UploadService uploadService;

    @Autowired
    public UploadController(UploadService uploadService, Environment environment) {
        this.uploadService = uploadService;
        this.avatarUploadDir = Objects.requireNonNull(environment.getProperty("resources.avatar-locations")).split(":", 2)[1];
        this.audioUploadDir = Objects.requireNonNull(environment.getProperty("resources.audio-locations")).split(":", 2)[1];
        this.videoUploadDir = Objects.requireNonNull(environment.getProperty("resources.video-locations")).split(":", 2)[1];
        this.pptUploadDir = Objects.requireNonNull(environment.getProperty("resources.ppt-locations")).split(":", 2)[1];
        System.out.println("上传文件夹：" + avatarUploadDir);
        System.out.println("音频文件夹：" + audioUploadDir);
        System.out.println("PPT文件夹：" + pptUploadDir);
        if (avatarUploadDir != null) {
            File file = new File(avatarUploadDir);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    System.out.println("创建上传文件夹失败！");
                }
            }
        }
        if (audioUploadDir != null) {
            File file = new File(audioUploadDir);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    System.out.println("创建音频文件夹失败！");
                }
            }
        }
        if (videoUploadDir != null) {
            File file = new File(videoUploadDir);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    System.out.println("创建视频文件夹失败！");
                }
            }
        }
        if (pptUploadDir != null) {
            File file = new File(pptUploadDir);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    System.out.println("创建PPT文件夹失败！");
                }
            }
        }
    }

    /**
     * 上传图片接口
     *
     * @param file 图片文件
     * @return 返回上传状态
     */
    @PostMapping("/picture")
    public Reply uploadPicture(@RequestParam("file") MultipartFile file) {
        String fileName = uploadService.uploadFile(file, avatarUploadDir);
        if (fileName == null) {
            return new Reply("上传图片").error(1, "上传失败");
        }
        return new Reply("上传图片").ok(0, "成功").put("name", fileName);
    }

    /**
     * 上传音频接口
     *
     * @param file 音频文件
     * @return 返回上传状态
     */
    @PostMapping("/audio")
    public Reply uploadAudio(@RequestParam("file") MultipartFile file) {
        String fileName = uploadService.uploadFile(file, audioUploadDir);
        if (fileName == null) {
            return new Reply("上传音频").error(1, "上传失败");
        }
        return new Reply("上传音频").ok(0, "成功").put("name", fileName);
    }

    /**
     * 上传PPT接口
     *
     * @param file PPT文件
     * @return 返回上传状态
     */
    @PostMapping("/ppt")
    public Reply uploadPPT(@RequestParam("file") MultipartFile file) {
        String fileName = uploadService.uploadFile(file, pptUploadDir);
        if (fileName == null) {
            return new Reply("上传PPT").error(1, "上传失败");
        }
        return new Reply("上传PPT").ok(0, "成功").put("name", fileName);
    }

    /**
     * 上传视频分片接口
     *
     * @param chunk      视频分片
     * @param index      视频分片的索引
     * @param chunkTotal 视频分片总数
     * @param fileSize   文件总大小
     * @param fileName   文件名
     * @param chunkSize  分片大小
     * @return 返回上传状态
     */
    @PostMapping("/chunk")
    public Reply uploadChunk(@RequestParam("chunk") MultipartFile chunk,
                             @RequestParam("index") int index,
                             @RequestParam("chunkTotal") int chunkTotal,
                             @RequestParam("fileSize") long fileSize,
                             @RequestParam("fileName") String fileName,
                             @RequestParam("chunkSize") long chunkSize,
                             @RequestParam("type") String type
    ) {
        int code = uploadService.uploadChunk(chunk, index, chunkTotal, fileSize, fileName, chunkSize, Objects.equals(type, "video") ? videoUploadDir : pptUploadDir);
        switch (code) {
            case 0 -> {
                return new Reply("上传视频分片").ok(code, "保存视频成功");
            }
            case 1 -> {
                return new Reply("上传视频分片").error(code, "保存分片失败");
            }
            case 2 -> {
                return new Reply(null).ok(code, "上传分片" + index + "/" + chunkTotal + ", " + fileName);
            }
            default -> {
                return new Reply("上传视频分片").error(code, "其他错误");
            }
        }
    }

    // TODO
    @PostMapping("/delete")
    Reply deleteFile(@RequestParam("fileName") String fileName) {
        int code = uploadService.deleteFile(avatarUploadDir + fileName);
        switch (code) {
            case 0 -> {
                return new Reply("删除文件 " + fileName).ok(code, "成功");
            }
            case 1 -> {
                return new Reply("删除文件 " + fileName).error(code, "文件名错误");
            }
            case 2 -> {
                return new Reply("删除文件 " + fileName).error(code, "文件删除失败");
            }
            default -> {
                return new Reply("删除文件 " + fileName).error(3, "其他错误");
            }
        }
    }
}
